﻿@using Masa.Blazor.Presets

<div class="text-center">
    <MSelect @bind-Value="_precision"
             Label="Precision"
             Items="@Items"
             ItemText="i => i.ToString()"
             ItemValue="i => i"
             Class="d-inline-block"
             Style="max-width: 180px">
    </MSelect>

    <PMobileDatePicker @bind-Value="_date" Precision="_precision" @key="_precision">
        <ActivatorContent>
            <MButton Color="primary" Class="text-capitalize" Text @attributes="@context.Attrs">
                @(ComputedDate)
            </MButton>
        </ActivatorContent>
    </PMobileDatePicker>
</div>

@code {

    [CascadingParameter(Name = "TimezoneOffset")]
    private TimeSpan TimezoneOffset { get; set; }

    private DateOnly _date;
    private DatePrecision _precision = DatePrecision.Day;

    private readonly static List<DatePrecision> Items = new()
    {
        DatePrecision.Year,
        DatePrecision.Month,
        DatePrecision.Day
    };

    private DateOnly ComputedDate => _precision switch
    {
        DatePrecision.Year => new DateOnly(_date.Year, 1, 1),
        DatePrecision.Month => new DateOnly(_date.Year, _date.Month, 1),
        _ => _date
    };

    protected override void OnInitialized()
    {
        _date = DateOnly.FromDateTime(DateTime.UtcNow.Add(TimezoneOffset));
    }

}
